﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TDT.Land.Entity;
using TDT.Land.BusinessLogic;
using TDT.Land.Utilities;
using TDT.Land.Models;

namespace TDT.Land.Web.Controllers
{
    public class UserController : BaseController
    {
        //
        // GET: /User/

        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public JsonResult LoginAction(LogOnModel model)
        {
            bool status = false;
            string msg = string.Empty;
            if (model == null || model.UserName == null || model.Password == null || model.UserName.Trim() == string.Empty || model.Password.Trim() == string.Empty)
            {
                status = false;
                msg = Messages.PlsInputInfomationLogon.Translate();
            }
            else
            {
                Sys_User loginValid = (new AdminRepo()).Login(model.UserName, model.Password);

                if (loginValid != null)
                {
                    UserNameFull = loginValid.UserName;
                    UserName = loginValid.LoginName;
                    UserID = loginValid.ID;
                    UserTypeSess = loginValid.UserType;
                    UserPermission = loginValid.IsLevel ?? 0;

                    status = true;
                    msg = string.Empty;
                }
                else
                {
                    status = false;
                    msg = Messages.WrongUsernameAndPassword.Translate();
                }
            }

            return Json(new
            {
                Result = status,
                Msg = msg
            });
        }
        public ActionResult LogoutAction()
        {
            UserNameFull = string.Empty;
            UserName = string.Empty;
            UserID = Guid.Empty;
            UserTypeSess = string.Empty;
            UserPermission = 0;
           // return RedirectToAction("LoginView");
            return Content("true");

        }

        public ActionResult UserLogin()
        {

            return PartialView("Us_Uc_Login");
        }


        public ActionResult RegisterUser()
        {

            return PartialView("UC_Us_RegisterUser");
        }
        
        public ActionResult Do_RegisterUser(Sys_User model)
        {
            string _status = "01";
            bool result = false;
            if (model.ID == null || model.ID == Guid.Empty) // Tạo mới
            {
                bool CheckUserName = AdminRepo.Instance.CheckUserName(model.LoginName);
                if (!CheckUserName)
                {
                    model.ID = Guid.NewGuid();
                    model.DateChangePass = DateTime.Today.Date;
                    model.DateCreate = DateTime.Today.Date;
                    model.IsActivate = true;
                    model.UserType = UserType.E_CUSTOMER;
                    model.IsLevel = 1;
                    model.UserCreate = model.UserName;
                    result = AdminRepo.Instance.CreateUser(model, UserName);
                    if (result)
                        _status = "00";
                }else
                    _status = "02";
            }
            else //Chinh sua
            {                
                model.UserType = UserType.E_CUSTOMER;
                model.IsLevel = 1;
                result = AdminRepo.Instance.UpdateUser(model, UserName);
                if (result)
                    _status = "00";
            }
            return Content(_status);
        }

    }
}
